package bai1;
import java.util.Scanner; 

public class Rational {
	int numberator,denominator;

	public Rational() {
		this.numberator = 0;
		this.denominator = 1;
	}

	public Rational(int numberator, int denominator) {
		if (denominator<0){
			this.numberator=-this.numberator;
			this.denominator=-this.denominator;
		}
		if (denominator==0) System.out.println("Loi mau so =0");
		this.numberator = numberator;
		this.denominator = denominator;
	}

	public int getNumberator() {
		return numberator;
	}

	public void setNumberator(int numberator) {
		this.numberator = numberator;
	}

	public int getDenominator() {
		return denominator;
	}

	public void setDenominator(int denominator) {
		this.denominator = denominator;
	}

	public static void reduce(int a, int b){
		while(a!=b){
			if(a>b) a=a-b;
			if(b>a) b=b-a;
		}
	}
	
	public static void reduce(Rational r){
		reduce (r.numberator,r.denominator);
	}

	public static void reciprocal(Rational a){
		int temp;
		if (a.numberator<0){
			a.numberator=-a.numberator;
			a.denominator=-a.denominator;
			System.out.print("\nPs da dao nguoc: "+a.numberator+"/"+a.denominator);
		}
		if (a.numberator>0){
			temp=a.numberator;
			a.numberator=a.denominator;
			a.denominator=temp;
			System.out.print("\nPs da dao nguoc: "+a.numberator+"/"+a.denominator);
		}
		else System.out.println("Loi tu so = 0");
	}

	public static Rational add(Rational a, Rational b, int i) {
		int c=a.numberator*b.denominator+b.numberator*a.denominator;
		int d=a.denominator*b.denominator;
		reduce(c,d);
		if(i==1) {
			if (d==1) System.out.print("\nTong 2 ps da cho: "+c);
			else System.out.print("\nTong 2 ps da cho: "+c+"/"+d);
		}
		return new Rational(c,d);
	}
	
	public static Rational subtract(Rational a, Rational b, int i) {
		int c=a.numberator*b.denominator-b.numberator*a.denominator;
		int d=a.denominator*b.denominator;
		reduce(c,d);
		if( i==1) {
			if (d==1) System.out.print("\nHieu 2 ps da cho: "+c);
			else System.out.print("\nHieu 2 ps da cho: "+c+"/"+d);
		}
		return new Rational(c,d);
	}
	
	public static Rational multiply(Rational a, Rational b, int i) {
		int c=a.numberator*b.numberator;
		int d=a.denominator*b.denominator;
		reduce(c,d);
		if( i==1) {
			if (d==1) System.out.print("\nTich 2 ps da cho: "+c);
			else System.out.print("\nTich 2 ps da cho: "+c+"/"+d);
		}
		return new Rational(c,d);
	}
	
	public static Rational divide(Rational a, Rational b, int i) {
		int c=a.numberator*b.denominator;
		int d=a.denominator*b.numberator;
		reduce(c,d);
		if( i==1) {
			if (d==1) System.out.print("\nTThuong 2 ps da cho: "+c);
			else System.out.print("\nThuong 2 ps da cho: "+c+"/"+d);
		}
		return new Rational(c,d);
	}

	public static void imp(Rational r, Scanner s) {
        System.out.print("Nhap tu so 1 : ");
        r.numberator=s.nextInt();
        System.out.print("Nhap mau so 1: ");
        r.denominator=s.nextInt();
        
        while (r.denominator==0) {
        	System.out.print("Nhap lai mau so: ");
        	r.setDenominator(s.nextInt());
        }
        reduce(r);
	}
	
	public static void main(String[] args)
	{
        Scanner src=new Scanner(System.in);
        Rational r1=new Rational();
        imp(r1,src);
        Rational r2=new Rational();
        imp(r2,src);
        add(r1,r2,1);
        subtract(r1,r2,1);
        multiply(r1,r2,1);
        divide(r1,r2,1);
        reciprocal(r1);
        src.close();
	}
}
